
package com.wyzz.hr.action;

import java.io.File;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.BaseDownloadAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.IEmpEvalBo;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Empeval;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.Pager;

public class EmpEval extends BaseDownloadAction {

	public EmpEval() {
		page = new Pager();
		outputIoName = "OEmpEval";
		searchOrExport = null;
	}

	private void setDateArray() {
		Calendar gc = Calendar.getInstance();
		gc.add(1, -1);
		sdateFrom[0] = DateUtil.getFirstDayInYear(gc).getTime();
		sdateTo[0] = DateUtil.getLastDayInYear(gc).getTime();
		gc.add(1, 1);
		gc.add(2, -6);
		sdateFrom[2] = DateUtil.getFirstDayInHalfYear(gc).getTime();
		sdateTo[2] = DateUtil.getLastDayInHalfYear(gc).getTime();
		gc.add(2, 6);
		gc.add(2, -3);
		sdateFrom[4] = DateUtil.getFirstDayInQuartz(gc).getTime();
		sdateTo[4] = DateUtil.getLastDayInQuartz(gc).getTime();
		gc.add(2, 3);
		gc.add(2, -1);
		sdateFrom[6] = DateUtil.getFirstDayInMonth(gc).getTime();
		sdateTo[6] = DateUtil.getLastDayInMonth(gc).getTime();
		gc.add(2, 1);
		sdateFrom[1] = DateUtil.getFirstDayInYear(gc).getTime();
		sdateTo[1] = DateUtil.getLastDayInYear(gc).getTime();
		sdateFrom[3] = DateUtil.getFirstDayInHalfYear(gc).getTime();
		sdateTo[3] = DateUtil.getLastDayInHalfYear(gc).getTime();
		sdateFrom[5] = DateUtil.getFirstDayInQuartz(gc).getTime();
		sdateTo[5] = DateUtil.getLastDayInQuartz(gc).getTime();
		sdateFrom[7] = DateUtil.getFirstDayInMonth(gc).getTime();
		sdateTo[7] = DateUtil.getLastDayInMonth(gc).getTime();
	}

	public Integer getEeTypeDetail() {
		return eeTypeDetail;
	}

	public void setEeTypeDetail(Integer eeTypeDetail) {
		this.eeTypeDetail = eeTypeDetail;
	}

	public String list() throws Exception {
		getDrillDownList();
		evalList = searchEval();
		if ("export".equals(searchOrExport))
			return downloadToExcel(evalList, outputIoName, outmatchModelId,
					"employeeByEeEmpNo");
		else
			return "success";
	}

	private List searchEval() {
		DetachedCriteria dcEval = searchEval_DC();
		addCriteria(dcEval);
		IEmpEvalBo empEvalBo = (IEmpEvalBo) getBean("empEvalBo");
		return empEvalBo.findEval(dcEval, page, searchOrExport);
	}

	private DetachedCriteria searchEval_DC() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empeval.class);
		dc.createAlias(Empeval.PROP_EMPLOYEE_BY_EE_EMP_NO, "emp", 1);
		dc.createAlias(Empeval.PROP_EMPLOYEE_BY_EE_MANAGER, "manager", 1);
		dc.createAlias(Empeval.PROP_DEPARTMENT, "department", 1);
		dc.createAlias(Empeval.PROP_EE_PB_NO, "eePbNo", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), "emp.empDeptNo", 1);
		if ("export".equals(searchOrExport)) {
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_PB_NO).toString(),
					Employee.PROP_EMP_PB_NO, 1);
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_LOCATION_NO).toString(),
					Employee.PROP_EMP_LOCATION_NO, 1);
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_TYPE).toString(), Employee.PROP_EMP_TYPE,
					1);
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_BENEFIT_TYPE).toString(),
					Employee.PROP_EMP_BENEFIT_TYPE, 1);
		}
		return dc;
	}

	public void addCriteria(DetachedCriteria dc) {
		if ("SUB".equals(super.authorityCondition))
			addSubDC(dc, "emp", 1);
		addOrders(dc, page,
				new String[] {
						(new StringBuilder())
								.append(Empeval.PROP_EE_START_DATE).append(
										"-down").toString(),
						(new StringBuilder()).append("emp.").append(
								Employee.PROP_EMP_DISTINCT_NO).append("-up")
								.toString() });
		BaseCrit.addEmpDC(dc, "emp", employeeId);
		BaseCrit.addDC(dc, Empeval.PROP_EE_TYPE, "eq", new String[] { eeType });
		BaseCrit.addDCDateRange(dc, Empeval.PROP_EE_START_DATE, eeStartDate,
				eeStartDateTo);
		BaseCrit.addDeptDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), (new StringBuilder())
				.append("emp.").append(Employee.PROP_EMP_PB_NO).toString(),
				null, new Department(departmentId));
		BaseCrit.addDeptDC(dc, Empeval.PROP_DEPARTMENT, Empeval.PROP_EE_PB_NO,
				null, new Department(eeDepartmentId));
		BaseCrit.addDCDateRange(dc, Empeval.PROP_EE_END_DATE, eeEndDate,
				eeEndDateTo);
		BaseCrit.addDC(dc, Empeval.PROP_EE_RATE, "eq",
				new String[] { eeRateSelect });
		BaseCrit.addEmpDC(dc, "manager", eeManager);
		BaseCrit.addDC(dc, Empeval.PROP_EE_COMMENTS, "like",
				new String[] { eeComments });
	}

	private void getDrillDownList() {
	}

	public String addEval() throws Exception {
		String currentEmpNo;
		IEmpEvalBo empEvalBo;
		Empeval empeval;
		if (StringUtils.isEmpty(eeType))
			return "error";
		currentEmpNo = getCurrentEmpNo();
		if (StringUtils.isEmpty(employeeId))
			employeeId = currentEmpNo;
		if ("SUB".equals(super.authorityCondition) && !checkAuth(employeeId)) {
			addErrorInfo("\u60A8\u6CA1\u6709\u65B0\u589E\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "success";
		}
		Calendar date = Calendar.getInstance();
		String tmpToday = (new StringBuilder()).append("").append(date.get(1))
				.append(date.get(2)).append(date.get(5)).toString();
		if (!tmpToday.equals(today)) {
			setDateArray();
			today = tmpToday;
		}
		empEvalBo = (IEmpEvalBo) getBean("empEvalBo");
		if (eeType.equals("\u8BD5\u7528\u671F")
				&& empEvalBo.hasPprobationaryPeriodEvalRecord(employeeId)) {
			addErrorInfo("\u8BD5\u7528\u671F\u8003\u8BC4\u53EA\u5141\u8BB8\u6DFB\u52A0\u4E00\u6B21\uFF01");
			return "error";
		}
		eeType = eeType.trim();
		if (!eeType.equals("\u9879\u76EE")
				&& !eeType.equals("\u8BD5\u7528\u671F")) {
			eeStartDate = sdateFrom[eeTypeDetail.intValue()];
			eeEndDate = sdateTo[eeTypeDetail.intValue()];
		}
		empeval = new Empeval();
		if (fileFileName != null && !"".equals(fileFileName)) {
			String UploadResult;
			String pathConfig = "sys.profile.file.path";
			String typeConfig = "sys.profile.file.type";
			String lengthConfig = "sys.profile.file.length";
			String postfix = fileFileName.substring(fileFileName
					.lastIndexOf("."));
			fileFileName = (new StringBuilder()).append(UUID.randomUUID())
					.append(postfix).toString();
			UploadResult = FileOperate.buildFile(file, pathConfig, typeConfig,
					lengthConfig, new String[] { fileFileName });
			if ("property".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u4E0A\u4F20\u9519\u8BEF\uFF01");
				return "error";
			}
			if ("fileExtendNameError".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u540E\u7F00\u540D\u4E0D\u5408\u6CD5\uFF01");
				return "error";
			}
			try {
				empeval.setEeAttachment(fileFileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		empeval.setDepartment(new Department(departmentId));
		empeval.setEeComments(eeComments);
		empeval.setEeEndDate(eeEndDate);
		empeval.setEePbNo(new PositionBase(eePbNo));
		empeval.setEeRate(eeRate);
		empeval.setEeStartDate(eeStartDate);
		empeval.setEeType(eeType);
		empeval.setEmployeeByEeEmpNo(new Employee(employeeId));
		empeval.setEmployeeByEeManager(new Employee(eeManager));
		empeval.setEeCreateBy(currentEmpNo);
		empeval.setEeCreateDate(new Date());
		empeval.setEeLastChangeBy(currentEmpNo);
		empeval.setEeLastChangeTime(new Date());
		empEvalBo.insert(empeval, getCurrentEmp());
		addActionMessage("\u65B0\u589E\u8003\u8BC4\u4FE1\u606F\u6210\u529F\u3002");
		return "success";
	}

	public String deleteEval(String eeId) {
		String auth = DWRUtil.checkAuth("EmpEval", "deleteEval");
		if ("error".equalsIgnoreCase(auth))
			return "noauth";
		IEmpEvalBo empConstractBo = (IEmpEvalBo) getBean("empEvalBo");
		Empeval ee = empConstractBo.getById(eeId);
		if (ee == null)
			return "noId";
		if ("SUB".equals(auth) && !checkAuth(ee.getEmployeeByEeEmpNo().getId()))
			return "noauth";
		String fileName = ee.getEeAttachment();
		if (fileName != null && !"".equals(fileName))
			FileOperate.deleteFile("sys.profile.file.path", fileName);
		empConstractBo.delete(eeId);
		return "success";
	}

	public String updateEval() throws Exception {
		String currentEmpNo;
		Empeval empeval;
		currentEmpNo = getCurrentEmpNo();
		if (employeeId == null || "".equals(employeeId))
			employeeId = currentEmpNo;
		if ("SUB".equals(super.authorityCondition) && !checkAuth(employeeId)) {
			addActionError("\u60A8\u6CA1\u6709\u4FEE\u6539\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		empeval = new Empeval();
		if (fileFileName != null && !"".equals(fileFileName)) {
			String UploadResult;
			String pathConfig = "sys.profile.file.path";
			String typeConfig = "sys.profile.file.type";
			String lengthConfig = "sys.profile.file.length";
			String postfix = fileFileName.substring(fileFileName
					.lastIndexOf("."));
			fileFileName = (new StringBuilder()).append(UUID.randomUUID())
					.append(postfix).toString();
			UploadResult = FileOperate.buildFile(file, pathConfig, typeConfig,
					lengthConfig, new String[] { fileFileName,
							empeval.getEeAttachment() });
			if ("property".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u4E0A\u4F20\u9519\u8BEF\uFF01");
				return "error";
			}
			if ("fileExtendNameError".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u540E\u7F00\u540D\u4E0D\u5408\u6CD5\uFF01");
				return "error";
			}
			try {
				empeval.setEeAttachment(fileFileName);
				fileFileName = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		empeval.setEeComments(eeComments);
		empeval.setEeRate(eeRate);
		empeval.setEmployeeByEeManager(new Employee(eeManager));
		empeval.setEeLastChangeBy(currentEmpNo);
		empeval.setEeLastChangeTime(new Date());
		IEmpEvalBo empConstractBo = (IEmpEvalBo) getBean("empEvalBo");
		empConstractBo.update(empeval, updateEeId, getCurrentEmp());
		addActionMessage("\u66F4\u65B0\u8003\u8BC4\u4FE1\u606F\u6210\u529F\u3002");
		eePbNo = null;
		eeRate = null;
		eeComments = null;
		eeManager = null;
		updateEeId = null;
		employeeId = null;
		return "success";
	}

	public String attachDelete() throws Exception {
		if (updateEeId == null || updateEeId.equals("") || fileFileName == null
				|| "".equals(fileFileName)) {
			addActionError("\u53C2\u6570\u4F20\u9012\u9519\u8BEF\uFF01");
			return "error";
		}
		IEmpEvalBo empConstractBo = (IEmpEvalBo) getBean("empEvalBo");
		Empeval ee = empConstractBo.getById(updateEeId);
		if (ee == null) {
			addActionError("\u53C2\u6570\u4F20\u9012\u9519\u8BEF\uFF01");
			return "error";
		}
		if ("SUB".equals(super.authorityCondition)
				&& !checkAuth(ee.getEmployeeByEeEmpNo().getId())) {
			addActionError("\u60A8\u6CA1\u6709\u5220\u9664\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		if (!empConstractBo.deleteAttach(updateEeId, fileFileName)) {
			addActionError("\u9644\u4EF6\u5220\u9664\u5931\u8D25\uFF01");
			return "error";
		} else {
			addActionMessage("\u9644\u4EF6\u5220\u9664\u6210\u529F\u3002");
			return "success";
		}
	}

	public String getEePbNo() {
		return eePbNo;
	}

	public void setEePbNo(String eePbNo) {
		this.eePbNo = eePbNo;
	}

	public String getEeType() {
		return eeType;
	}

	public void setEeType(String eeType) {
		this.eeType = eeType;
	}

	public Date getEeStartDate() {
		return eeStartDate;
	}

	public void setEeStartDate(Date eeStartDate) {
		this.eeStartDate = eeStartDate;
	}

	public Date getEeEndDate() {
		return eeEndDate;
	}

	public void setEeEndDate(Date eeEndDate) {
		this.eeEndDate = eeEndDate;
	}

	public String getEeRate() {
		return eeRate;
	}

	public void setEeRate(String eeRate) {
		this.eeRate = eeRate;
	}

	public String getEeComments() {
		return eeComments;
	}

	public void setEeComments(String eeComments) {
		this.eeComments = eeComments;
	}

	public String getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}

	public String getEeManager() {
		return eeManager;
	}

	public void setEeManager(String eeManager) {
		this.eeManager = eeManager;
	}

	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

	public String getEmpNo() {
		return empNo;
	}

	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}

	public String getUpdateEeId() {
		return updateEeId;
	}

	public void setUpdateEeId(String updateEeId) {
		this.updateEeId = updateEeId;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getEeDepartmentId() {
		return eeDepartmentId;
	}

	public void setEeDepartmentId(String eeDepartmentId) {
		this.eeDepartmentId = eeDepartmentId;
	}

	public List getManagerList() {
		return managerList;
	}

	public void setManagerList(List managerList) {
		this.managerList = managerList;
	}

	public List getEvalList() {
		return evalList;
	}

	public void setEvalList(List evalList) {
		this.evalList = evalList;
	}

	public Date getEeStartDateTo() {
		return eeStartDateTo;
	}

	public void setEeStartDateTo(Date eeStartDateTo) {
		this.eeStartDateTo = eeStartDateTo;
	}

	public Date getEeEndDateTo() {
		return eeEndDateTo;
	}

	public void setEeEndDateTo(Date eeEndDateTo) {
		this.eeEndDateTo = eeEndDateTo;
	}

	public String getEeRateSelect() {
		return eeRateSelect;
	}

	public void setEeRateSelect(String eeRateSelect) {
		this.eeRateSelect = eeRateSelect;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public String getOutmatchModelId() {
		return outmatchModelId;
	}

	public void setOutmatchModelId(String outmatchModelId) {
		this.outmatchModelId = outmatchModelId;
	}

	public String getOutputIoName() {
		return outputIoName;
	}

	public void setOutputIoName(String outputIoName) {
		this.outputIoName = outputIoName;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	public String getEeDepartmentName() {
		return eeDepartmentName;
	}

	public void setEeDepartmentName(String eeDepartmentName) {
		this.eeDepartmentName = eeDepartmentName;
	}

	private static final long serialVersionUID = 1L;
	private String eePbNo;
	private String eeType;
	private Date eeStartDate;
	private Date eeEndDate;
	private Date eeStartDateTo;
	private Date eeEndDateTo;
	private String eeRate;
	private String eeRateSelect;
	private String eeComments;
	private String fileFileName;
	private File file;
	private String departmentId;
	private String departmentName;
	private String eeDepartmentName;
	private String eeDepartmentId;
	private String eeManager;
	private String employeeId;
	private String empNo;
	private String updateEeId;
	private List managerList;
	private List evalList;
	private static Date sdateFrom[] = new Date[8];
	private static Date sdateTo[] = new Date[8];
	private Pager page;
	private static String today;
	private String outmatchModelId;
	private String outputIoName;
	private String searchOrExport;
	private Integer eeTypeDetail;

}
